<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>宿舍评分 - {{ $dormitory->code }}</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <style>
        .container {
            padding: 15px;
        }

        .dormitory-header {
            padding: 15px 0;
            border-bottom: 1px solid #eee;
            margin-bottom: 20px;
        }

        .dormitory-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .rating-date {
            font-size: 14px;
            color: #666;
            margin-top: 5px;
        }

        .category-section {
            margin-bottom: 20px;
            border: 1px solid #eee;
            border-radius: 4px;
            padding: 15px;
        }

        .category-title {
            font-size: 16px;
            font-weight: bold;
            color: #333;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
            margin-bottom: 15px;
        }

        .rating-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #f5f5f5;
        }

        .item-name {
            font-size: 14px;
            color: #333;
            max-width: 60%;
        }

        .score-input {
            display: flex;
            align-items: center;
        }

        .max-score {
            margin-left: 10px;
            font-size: 12px;
            color: #999;
        }

        .remark-section {
            margin-top: 20px;
        }

        .remark-title {
            font-size: 14px;
            font-weight: bold;
            color: #333;
            margin-bottom: 10px;
        }

        .total-score {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            padding: 15px;
            font-size: 16px;
            margin-top: 20px;
            border-top: 1px solid #eee;
        }

        .total-label {
            color: #333;
            margin-right: 10px;
        }

        .total-value {
            color: #FF5722;
            font-weight: bold;
        }

        .nav-buttons {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
        }

        .nav-buttons .layui-btn {
            width: 48%;
        }

        .nav-buttons .layui-btn-disabled {
            opacity: 0.6;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 导航按钮 -->
        <div class="nav-buttons">
            <button type="button" class="layui-btn layui-btn-primary prev-btn" id="prevBtn"
                {{ $hasPrev ? '' : 'disabled' }}>上一个</button>
            <button type="button" class="layui-btn layui-btn-primary next-btn" id="nextBtn"
                {{ $hasNext ? '' : 'disabled' }}>下一个</button>
        </div>

        <div class="dormitory-header">
            <div class="dormitory-title">宿舍评分 - {{ $dormitory->code }}</div>
            <div class="rating-date">评分日期: {{ $currentDate }}</div>
        </div>

        <form class="layui-form rating-form" id="ratingForm">
            @csrf
            @if (isset($rating))
                <input type="hidden" name="rating_id" value="{{ $rating->id }}">
            @endif
            <input type="hidden" name="dormitory_id" value="{{ $dormitory->id }}">
            <input type="hidden" name="period_id" value="{{ $period->id }}">

            <!-- 评分分类 -->
            @foreach ($ratingCategories as $category)
                <div class="category-section">
                    <div class="category-title">{{ $category->category_name }} ({{ $category->total_score }}分)</div>

                    @foreach ($category->ratingSubitems as $item)
                        <div class="rating-item">
                            <div class="item-name">{{ $item->subitem_name }}</div>
                            <div class="score-input">
                                <input type="number" name="scores[{{ $item->id }}]"
                                    value="{{ $scores[$item->id] ?? 0 }}" min="0"
                                    max="{{ $item->subitem_score }}" class="layui-input score-input-field"
                                    style="width: 60px; text-align: center;" onchange="calculateTotal()">
                                <div class="max-score">/{{ $item->subitem_score }}</div>
                            </div>
                        </div>
                    @endforeach
                </div>
            @endforeach

            <!-- 备注信息 -->
            <div class="remark-section">
                <div class="remark-title">备注信息:</div>
                <textarea name="remark" placeholder="请输入备注信息..." class="layui-textarea" maxlength="500">{{ $rating->remark ?? '' }}</textarea>
            </div>

            <!-- 总分统计 -->
            <div class="total-score">
                <div class="total-label">总分:</div>
                <div class="total-value"><span id="totalScore">{{ $totalScore ?? 0 }}</span>分</div>
            </div>

            <!-- 提交按钮 -->
            <div class="layui-form-item" style="margin-top: 20px;">
                <button type="button" class="layui-btn layui-btn-normal" lay-submit
                    lay-filter="formSubmit">提交评分</button>
            </div>
        </form>
    </div>

    <script src="/layui/layui.js"></script>
    <script>
        layui.use(['form', 'layer', 'jquery'], function() {
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.jquery;
            var totalScore = {{ $totalScore ?? 0 }};

            // 计算总分
            window.calculateTotal = function() {
                totalScore = 0;
                $('.score-input-field').each(function() {
                    var score = parseInt($(this).val()) || 0;
                    totalScore += score;
                });
                $('#totalScore').text(totalScore);
            };

            // 表单提交
            form.on('submit(formSubmit)', function(data) {
                if (totalScore === 0) {
                    layer.msg('请至少填写一项评分', {
                        icon: 2
                    });
                    return false;
                }

                var loading = layer.load(2);
                $.ajax({
                    url: "{{ isset($rating) ? route('ratings.update', $rating->id) : route('ratings.store') }}",
                    type: "{{ isset($rating) ? 'PUT' : 'POST' }}",
                    data: $('#ratingForm').serialize(),
                    success: function(res) {
                        layer.close(loading);
                        if (res.success) {
                            layer.msg(res.message, {
                                icon: 1
                            }, function() {
                                // 更新成功后跳转或刷新
                                window.location.href =
                                    "{{ route('dormitories.index') }}";
                            });
                        } else {
                            layer.msg(res.message || '提交失败', {
                                icon: 2
                            });
                        }
                    },
                    error: function(xhr) {
                        layer.close(loading);
                        var errors = xhr.responseJSON.errors;
                        if (errors) {
                            var errorMsg = '';
                            for (var key in errors) {
                                errorMsg += errors[key][0] + '<br>';
                            }
                            layer.msg(errorMsg, {
                                icon: 2
                            });
                        } else {
                            layer.msg('网络错误，请稍后重试', {
                                icon: 2
                            });
                        }
                    }
                });
                return false;
            });

            // 上一个宿舍
            $('#prevBtn').on('click', function() {
                window.location.href =
                    "{{ route('ratings.create', ['dormitory_id' => $prevDormitoryId]) }}";
            });

            // 下一个宿舍
            $('#nextBtn').on('click', function() {
                window.location.href =
                    "{{ route('ratings.create', ['dormitory_id' => $nextDormitoryId]) }}";
            });
        });
    </script>
</body>

</html>
